// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import flexible from './utils/flexible'
import store from './store'
import router from './router'
import common from './utils/common'
import filters from './utils/filters'
import axios from './utils/request'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import {ToastPlugin, AlertPlugin, LoadingPlugin, WechatPlugin, ConfirmPlugin} from 'vux'
import {IndexList, IndexSection} from 'mint-ui'
import 'mint-ui/lib/style.css'
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
import './style/reset.less'
import './style/style.less'

let options = {
    fullscreenEl: false
}

Vue.use(ElementUI)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(LoadingPlugin)
Vue.use(WechatPlugin)
Vue.use(ConfirmPlugin)
Vue.component(IndexList.name, IndexList)
Vue.component(IndexSection.name, IndexSection)

Vue.use(preview, options)
Vue.use(preview)

Vue.config.productionTip = false
Vue.prototype.$http = axios
Vue.prototype.$method = common
Vue.prototype.$store = store
Vue.prototype.$filter = filters

// 全局引入过滤器
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})
// 页面自适应
flexible.remDesign(750, 1920)

router.beforeEach((to, from, next) => {
    if (!window.wxurl) {
        window.wxurl = window.location.href
    }

    if (to.query.wxCode && window.wxCode !== to.query.wxCode) {
        window.wxCode = to.query.wxCode
        common.setCookie('wxCode', to.query.wxCode)
    }
    // next()
    if (to.path === '/login') {
        next()
    } else {
        setTimeout(() => {
            if (window.wxCode) {
                if (!store.getters.wxLogin) {
                    store.dispatch('loginByWX').then(code => {
                        if (Number(code) === -999) {
                            next('/login')
                        } else if (Number(code) !== 0) {
                            next(false)
                        } else {
                            routeRedirect(next, store.getters.user, to, true)
                        }
                    })
                } else {
                    routeRedirect(next, store.getters.user, to)
                }
            } else {
                if (!store.getters.user) {
                    if (common.getUrlParam('code')) {
                        if (!store.getters.wxOaLogin) {
                            let code = common.getUrlParam('code')
                            store.dispatch('loginBySession').then(res => {
                                if (!res) {
                                    store.dispatch('loginByWXOa', code).then(res => {
                                        if (Number(res) !== 0) {
                                            next(false)
                                        } else {
                                            routeRedirect(next, store.getters.user, to, true)
                                        }
                                    })
                                } else {
                                    routeRedirect(next, store.getters.user, to)
                                }
                            })
                        } else {
                            routeRedirect(next, store.getters.user, to)
                        }
                    } else {
                        store.dispatch('loginBySession').then(res => {
                            if (!res) {
                                axios.get('/index/oaAppId').then(result => {
                                    if (result.success) {
                                        window.open('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + result.data + '&redirect_uri=' + encodeURI(location.href) + '&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect', '_self', '', true)
                                    }
                                })
                                // next('/login')
                            } else {
                                routeRedirect(next, store.getters.user, to, true)
                            }
                        })
                    }

                } else {
                    routeRedirect(next, store.getters.user, to)
                }
            }
        }, 0)

    }

    function routeRedirect(next, user, to, replace) {
        let obj = router.options.routes.find((item) => {
            return item.path === ('/')
        })
        if (obj && obj.path && to.path !== '/') {
            if (replace) {
                next({...to, replace: true})
            } else {
                next(...to)
            }
        } else {
            next({...to, replace})
        }
    }
})

router.afterEach((to, from) => {
    // let _href = window.location.href // 获取当前地址
    // if (_href.indexOf('reload=1') <= -1 && !window.viewReload) {
    //     reload()
    // } else {
    //     window.viewReload = true
    // }
    //
    // function reload() {
    //     if (_href.indexOf('?') >= 0) {
    //         _href += '&' + 'reload=1'
    //     } else {
    //         _href += '?' + 'reload=1'
    //     }
    //     window.location.replace(_href)
    // }
})

/* eslint-disable no-new */
window.$vm = new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<App/>'
})

window.ajaxPath = process.env.NODE_ENV !== 'production' ? 'http://192.168.1.12:8100' : location.origin
